import {
  find
} from "../../../api/product/"
import {
  list
} from "../../../api/chef"
import {
  buyNow
} from "../../../api/cart"
const util = require('../../../utils/util');
const app = getApp();
// 注意修改shopId
Page({

  /**
   * 页面的初始数据
   */
  data: {
    APP_OSSURL_API: app.APP_OSSURL_API,
    safeBottom: Math.abs(app.globalData.hh - (app.globalData.safeArea.bottom || 0)),
    specs: [],
    cooks: [],
    forms: [],
    adjust: true,
    orderId: 1,
    selectUnfold: false,
    id: '',
    shopId: '',
    imageList: [],
    props: [],
    products: [],
    chefTitle: '',
    chefId: '',
    chefIndex: [0],
    productsData: {
      specsTitle: '',
      formsTitle: '',
      productsId: '',
      price: 0,
      subTitle: '',
      name: ''
    },
    textareaTitle: '',
    showMask: 'none',
    menuSlide: 'none',
    packageNum: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // debugger
    console.log(options)
    this.data.id = options.id
    this.data.shopId = options.shopId
    this.loadFind()
    this.chefList()
    this.setData({
      id: this.data.id,
      shopId: this.data.shopId
    })
  },


  /**
   * 关闭已下拉的select,需要以捕获方式绑定事件
   */
  closeSelect: function () {
    this.setData({
      selectUnfold: false
    })
  },

  /**
   * 厨师选择/选择调配
   */
  checkAjust: function () {
    this.setData({
      adjust: !this.data.adjust
    })
  },
  /**
   * 预约/进入订单详情页面
   */
  toOrderDetail: function (e) {
    wx.navigateTo({
      url: `../KBOrderDetail/KBOrderDetail?orderId=${this.data.orderId}`,
    })
  },
  /**
   * 改变下拉菜单选项
   */
  changeOption: function (data) {
    var _this = this
    _this.data.forms = []
    _this.data.productsData.specsTitle = data.detail
    _this.data.products.forEach(function (item) {
      if (item.spec1 == data.detail) {
        _this.data.forms.push(item.spec2)
      }
      if (item.spec1 == _this.data.productsData.specsTitle && item.spec2 == _this.data.productsData.formsTitle) {
        _this.data.productsData.specsTitle = item.spec1
        _this.data.productsData.formsTitle = item.spec2
        _this.data.productsData.productsId = item.id
        _this.data.productsData.price = item.price
      }
    })
    _this.setData({
      forms: _this.data.forms.reverse(),
      productsData: _this.data.productsData
    })
  },

  changeOptionTwo: function (data) {
    var _this = this
    _this.data.specs = []
    _this.data.productsData.formsTitle = data.detail
    _this.data.products.forEach(function (item) {
      if (item.spec2 == data.detail) {
        _this.data.specs.push(item.spec1)
      }
      if (item.spec1 == _this.data.productsData.specsTitle && item.spec2 == _this.data.productsData.formsTitle) {
        _this.data.productsData.specsTitle = item.spec1
        _this.data.productsData.formsTitle = item.spec2
        _this.data.productsData.productsId = item.id
        _this.data.productsData.price = item.price
      }
    })
    _this.setData({
      specs: _this.data.specs.reverse(),
      productsData: _this.data.productsData
    })
  },
  /**
   * 更改选择弹框（规格和形式，数量）
   */
  optionChoose: function () {
    this.setData({
      showMask: 'specMask',
    })
    setTimeout(() => {
      this.setData({
        menuSlide: 'specMask'
      })
    }, 550)
  },
  /**
   * 更改选择弹框（规格和形式，数量）
   */
  chefChoose: function () {
    this.setData({
      showMask: 'chefMask',
    })
    setTimeout(() => {
      this.setData({
        menuSlide: 'chefMask'
      })
    }, 550)
  },
  // 套餐详情
  loadFind() {
    var _this = this
    var specs = []
    var forms = []
    find(this.data.id).then(res => {
      _this.data.imageList = res.data.meta.images.map(e => util.thumbnail(e, 750, 750))
      _this.data.props = res.data.meta.props
      _this.data.products = res.data.products
      _this.data.productsData.specsTitle = _this.data.products[0].spec1
      _this.data.productsData.formsTitle = _this.data.products[0].spec2
      _this.data.productsData.productsId = _this.data.products[0].id
      _this.data.productsData.price = _this.data.products[0].price
      _this.data.productsData.marketPrice = _this.data.products[0].marketPrice
      _this.data.productsData.thumbnail = _this.data.products[0].thumbnail
      _this.data.productsData.totalSale = _this.data.products[0].totalSale
      _this.data.productsData.subTitle = _this.data.products[0].subTitle
      _this.data.productsData.name = _this.data.products[0].name
      res.data.products.forEach(function (item) {
        specs.push(item.spec1)
        forms.push(item.spec2)
      })
      _this.data.specs = _this.unique(specs)
      _this.data.forms = _this.unique(forms)
      _this.setData({
        imageList: _this.data.imageList,
        props: _this.data.props,
        products: _this.data.products,
        specs: _this.data.specs,
        forms: _this.data.forms,
        productsData: _this.data.productsData
      })
    })
  },
  unique(arr) {
    if (!Array.isArray(arr)) {
      console.log('type error!')
      return
    }
    var array = [];
    for (var i = 0; i < arr.length; i++) {
      if (array.indexOf(arr[i]) === -1) {
        array.push(arr[i])
      }
    }
    return array;
  },
  chefList() {
    var _this = this
    list({
      shopId: _this.data.shopId
    }).then(res => {
      // debugger
      _this.data.cooks = res.data.data
      if (res.data.data.length > 0) {
        _this.data.chefTitle = res.data.data[0].name
        _this.data.chefId = res.data.data[0].id
      }
      _this.setData({
        cooks: _this.data.cooks,
        chefTitle: _this.data.chefTitle,
        chefId: _this.data.chefId
      })
    })
  },
  changeChef(data) {
    var _this = this
    // _this.data.chefTitle = data.detail.name
    // _this.data.chefId = data.detail.id
    _this.data.chefTitle = this.data.cooks[data.detail.value[0]].name
    _this.data.chefId = this.data.cooks[data.detail.value[0]].id
    console.log(_this.data.chefTitle, _this.data.chefId)
    _this.setData({
      chefTitle: _this.data.chefTitle,
      chefId: _this.data.chefId
    })
  },
  textareaClick(data) {
    console.log(data)
    var _this = this
    _this.data.textareaTitle = data.detail.value
  },
  buyNowClick() {
    var _this = this
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    buyNow({
      productId: _this.data.productsData.productsId,
      quantity: 1
    }).then(res => {
      console.log(res)
      wx.hideLoading()
      wx.navigateTo({
        url: "/pages/cook/KBOrderDetail/KBOrderDetail?chefId=" + this.data.chefId + "&textareaTitle=" + this.data.textareaTitle + "&shopId=" + _this.data.shopId + "&adjust=" + _this.data.adjust + "&packageNum=" + _this.data.packageNum + "&formsTitle=" + this.data.productsData.formsTitle + "&marketPrice=" + this.data.productsData.marketPrice
      })
    }).catch(error => {
      wx.hideLoading()
    })
  },
  maskclick() {
    this.setData({
      menuSlide: 'none'
    })
    setTimeout(() => {
      this.setData({
        showMask: 'none'
      })
    }, 550)
  },
  reducePackageNum() {
    if (this.data.packageNum <= 1) {
      wx.showToast({
        icon: 'none',
        title: '一桌起订',
      })
      return
    }
    this.setData({
      packageNum: --this.data.packageNum
    })
  },
  addPackageNum() {
    this.setData({
      packageNum: ++this.data.packageNum
    })
  }
})